{***************************************************************
    SpyGestion  Copyright (C) 2008 Christophe KOALI <nature2gas@gmail.com>
    
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
    
    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
    
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
****************************************************************}
unit uStatsPlayer;

interface

uses
  Windows, Messages, SysUtils,  Classes, Graphics, Controls, Forms,
  Dialogs, modStatsPlayers,
  sFonctionsChaines,
  ExtCtrls, TransparentPanel, StdCtrls, Buttons;

type
  TfrmInfoPlayer = class(TForm)
    panListe: TTransparentPanel;
    lstPlayer: TListBox;
    Label1: TLabel;
    panJoueur: TTransparentPanel;
    lblPlayerName: TLabel;
    lblPoints: TLabel;
    Label3: TLabel;
    lblPlace: TLabel;
    Label5: TLabel;
    Label6: TLabel;
    lblAllianceName: TLabel;
    Label9: TLabel;
    Label8: TLabel;
    lblShouldAttack: TLabel;
    cmdOK: TSpeedButton;
    cmdAnnuler: TSpeedButton;
    timHide: TTimer;
    Label2: TLabel;
    lblPointsFlottes: TLabel;
    procedure timHideTimer(Sender: TObject);
    procedure lblShouldAttackClick(Sender: TObject);
    procedure panJoueurClick(Sender: TObject);
    procedure lblPlayerNameClick(Sender: TObject);
    procedure cmdAnnulerClick(Sender: TObject);
    procedure lstPlayerDblClick(Sender: TObject);
    procedure cmdOKClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    // Permet de définir si on doit attaquer ou pas
    FCurrentPlayerPointTotal: integer;
    FCurrentPlayerPointFlottes : integer;


    // Simple copie de pointeur
    FCStatsPlayer: TCollectionPlayerStats;
    FCStatsPlayerFlotte: TCollectionPlayerStats;

    FAlliancesAlliees: TStrings;

    function GetSelectedItem: integer;
    procedure LoadSelectedItem;

    procedure PositionneCorrectement(bMakeSwitch: boolean = true);

    function IsAllianceAlliee(s: string): boolean;

    // Mettre devant toute les autres fenetre
    procedure Devant;
  public
    { Public declarations }
    procedure SetCurrentPlayerPointTotal(n: integer);
    procedure SetStatsCollectionTotal(n: TCollectionPlayerStats);


    procedure SetCurrentPlayerPointFlottes(n: integer);
    procedure SetStatsCollectionFlottes(n: TCollectionPlayerStats);

    procedure SetAlliancesAlliees(n: TStrings);




    procedure ShowPlayer(pe: PPlayerStats);
    procedure ShowListFromMaskRes;

  end;

var
  frmInfoPlayer: TfrmInfoPlayer;

implementation

{$R *.dfm}

// Source: http://delphi.developpez.com/faq/?page=interexecution#remettreappenavantplan
procedure TfrmInfoPlayer.Devant;
Var Proc,Nous:Integer;
begin
  Proc := GetWindowThreadProcessId(GetForeGroundWindow);
  Nous := GetCurrentThreadID;
  
  if Proc <> Nous then begin
     // Nous ne sommes pas l'application en premier plan
     AttachThreadInput(Nous, Proc, True);
     SetForeGroundWindow(Handle);
     AttachThreadInput(Nous, Proc, False);
     Application.BringToFront;

  end else begin
     // Nous sommes déjà en premier plan
     SetForeGroundWindow(Handle);
  end;

end;


function TfrmInfoPlayer.IsAllianceAlliee(s: string): boolean;
var i: integer;
    c: string;
begin

    Result := false;

    if (FAlliancesAlliees = nil) or (s = '') then begin
       exit;
    end;


    for i := 0 to FAlliancesAlliees.Count - 1 do begin
        c := trim(FAlliancesAlliees.Strings[i]);

        if c = '' then Continue;

        if SameText(s, c) then begin
           Result := true;
           exit;
        end;

    end;

end;


procedure TfrmInfoPlayer.SetAlliancesAlliees(n: TStrings);
begin
    FAlliancesAlliees := n;
end;

procedure TfrmInfoPlayer.PositionneCorrectement(bMakeSwitch: boolean = true);
begin
    Self.Top  := ((Screen.Height - Self.Height) div 2) - 50; // - 100;
    Self.Left := Screen.Width  - Self.Width - 30;

    if bMakeSwitch then begin
       FormStyle := fsNormal;
       Sleep(1);
       FormStyle := fsStayOnTop;
    end;


end;

function TfrmInfoPlayer.GetSelectedItem: integer;
var i: integer;
begin

   Result := -1;

   if lstPlayer.Items.Count = 0 then exit;


   for i := 0 to lstPlayer.Items.Count - 1 do begin
       if lstPlayer.Selected[i] then begin
          Result := i;
          break;
       end;

   end;


end;

procedure TfrmInfoPlayer.ShowListFromMaskRes;
var i: integer;
    pe : PPlayerStats;
begin
   if FCStatsPlayer = nil then exit;

   if FCStatsPlayer.MaskListResult.Count = 0 then exit;
   if FCStatsPlayer.MaskListResult.Count = 1 then begin
      ShowPlayer(FCStatsPlayer.MaskListResult.Items[0]);
      exit;
   end;

   lstPlayer.Items.Clear;

   for i := 0 to FCStatsPlayer.MaskListResult.Count - 1 do begin
      pe := FCStatsPlayer.MaskListResult.Items[i];
      lstPlayer.Items.Add(pe.Nom);
   end;

    panListe.Top     := 0;
    panListe.Left    := 8;
    panJoueur.Visible  := false;
    panListe.Visible := true;

    Height := panListe.Top + panListe.Height + 8 + 18;

    PositionneCorrectement;
    Show;
    PositionneCorrectement(False);

    SetFocus;
end;


procedure TfrmInfoPlayer.SetCurrentPlayerPointFlottes(n: integer);
begin
   FCurrentPlayerPointFlottes := n;
end;

procedure TfrmInfoPlayer.SetStatsCollectionFlottes(n: TCollectionPlayerStats);
begin
   FCStatsPlayerFlotte := n;
end;


procedure TfrmInfoPlayer.SetStatsCollectionTotal(n: TCollectionPlayerStats);
begin
    FCStatsPlayer := n;
end;


procedure TfrmInfoPlayer.SetCurrentPlayerPointTotal(n: integer);
begin
    FCurrentPlayerPointTotal := n;
end;

procedure TfrmInfoPlayer.ShowPlayer(pe: PPlayerStats);
var peFlotte: PPlayerStats;
begin

    lblPlayerName.Caption := pe.Nom;

    lblPlace.Caption := sfcFormatStrToMilliers(pe.Place);
    lblPoints.Caption := sfcFormatStrToMilliers(pe.Points);


    if FCStatsPlayerFlotte <> nil then begin
       peFlotte := FCStatsPlayerFlotte.Get_Pointeur(pe.Nom);
       if peFlotte = nil then begin
          lblPointsFlottes.Caption := '---';
       end else begin
          lblPointsFlottes.Caption := sfcFormatStrToMilliers(peFlotte.Points);
       end;
       
    end else
       peFlotte := nil;

    if FCurrentPlayerPointTotal > 0 then begin
       lblShouldAttack.Visible := true;

       if pe.Points < (FCurrentPlayerPointTotal) then begin
          lblShouldAttack.Caption := 'Positif';
          lblShouldAttack.Font.Color := clBlue;

       end else begin
          lblShouldAttack.Caption := 'Négatif';
          lblShouldAttack.Font.Color := clRed;


       end;

       if peFlotte <> nil then begin
           if peFlotte.Points > FCurrentPlayerPointFlottes then begin
              // trop de flotte
              lblShouldAttack.Caption := 'Négatif';
              lblShouldAttack.Font.Color := clRed;
           end;

       end;


    end else
       lblShouldAttack.Visible := false;

    
    if pe.Alliance <> '' then begin
       lblAllianceName.Caption := pe.Alliance;

       if IsAllianceAlliee(pe.Alliance) then begin
          lblShouldAttack.Caption := 'Allié';
          lblShouldAttack.Font.Color := clRed;
       end;

    end else begin
       lblAllianceName.Caption := '(aucune)';
    end;


    panJoueur.Top     := 0;
    panJoueur.Left    := 8;
    panListe.Visible  := false;
    panJoueur.Visible := true;

    Height := panJoueur.Top + panJoueur.Height + 8 + 18;

    PositionneCorrectement;

    timHide.Interval := 0;
    Sleep(1);
    timHide.Interval := 6500;


    Show;
    SetFocus;

    Sleep(1);
    FormStyle := fsStayOnTop;
    Show;
    PositionneCorrectement(False);
    SetFocus;


end;


procedure TfrmInfoPlayer.FormCreate(Sender: TObject);
begin
    FCurrentPlayerPointTotal := -1;
    FCurrentPlayerPointFlottes := -1;

    FCStatsPlayer := nil;
    FCStatsPlayerFlotte := nil;

    FAlliancesAlliees := nil;

    PositionneCorrectement;
end;

procedure TfrmInfoPlayer.LoadSelectedItem;
var i: integer;
    pe : PPlayerStats;
begin

   i := GetSelectedItem;

   if i < 0 then exit;

   if i >= FCStatsPlayer.MaskListResult.Count then exit;

   pe := FCStatsPlayer.MaskListResult.Items[i];

   ShowPlayer(pe);

end;

procedure TfrmInfoPlayer.cmdOKClick(Sender: TObject);
begin
    LoadSelectedItem;
end;

procedure TfrmInfoPlayer.lstPlayerDblClick(Sender: TObject);
begin
    LoadSelectedItem;
end;

procedure TfrmInfoPlayer.cmdAnnulerClick(Sender: TObject);
begin
    Hide;
end;

procedure TfrmInfoPlayer.lblPlayerNameClick(Sender: TObject);
begin
   Hide;
end;

procedure TfrmInfoPlayer.panJoueurClick(Sender: TObject);
begin
   Hide;
end;

procedure TfrmInfoPlayer.lblShouldAttackClick(Sender: TObject);
begin
    Hide;
end;

procedure TfrmInfoPlayer.timHideTimer(Sender: TObject);
begin
    timHide.Interval := 0;
    Hide;
end;

end.
